//
//  FHStatusViewModel.swift
//  FHweibo
//
//  Created by 张嵩雷 on 16/4/13.
//  Copyright © 2016年 feng. All rights reserved.
//

import UIKit

class FHStatusViewModel: NSObject {
    
    //单例
    static let sharedInstance: FHStatusViewModel = FHStatusViewModel()
    
    //加载微博数据
    func loadStatus(loadStatusCallback: (statuses: [FHStatus]?, error: NSError?) ->()) {
        //1.urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //2.参数
        let parameters = ["access_token": FHUserAccountViewModel.sharedInstance.userAccount!.access_token!]
        
        // 3.发送请求
        FHNetworkTool.sharedInstance.GET(urlString, parameters: parameters, progress: nil, success: { (task, responseObject) -> Void in
            
//            FHPrint("获取微博数据成功了: \(responseObject)")
           
            //获取微博数据
            if let dict = responseObject as? [String: AnyObject], let statusesDict = dict["statuses"] as? [[String: AnyObject]] {
                
                var statusModels: [FHStatus] = []
                //遍历每个字典
                for dict in statusesDict {
                    
                    //字典转模型
                    let status = FHStatus(dict: dict)
                    
                    //将模型添加到数组里面
                    statusModels.append(status)
                }
                
                loadStatusCallback(statuses: statusModels, error: nil)
            }
            
        
            
            }) { (task , error) -> Void in
                
                loadStatusCallback(statuses: nil, error: error)
        }
    }

}
